//index.js
//获取应用实例
const app = getApp() 
var util = require('../../utils/util.js');
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    userInfo: {},
    username: '',
    password: '',
    hasUserInfo: false,
    isAutoLogin: true,
    startDateTime: "",
    beTogetherDuration: "",
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  onLoad: function () {
    this.setData({
      userInfo: app.globalData.userInfo,
      hasUserInfo: true,
      "username": wx.getStorageSync('username'),
      "password": wx.getStorageSync('password'),
      "isAutoLogin": wx.getStorageSync('autologin')
    });
    if(this.data.username && this.data.password && this.data.isAutoLogin){
      this.Verification();
    }
    app.getBeTogetherDuration(this);
  },
  GetUsername (e) {
    this.setData({ username: e.detail.value});
    wx.setStorageSync('username', e.detail.value);
  },
  GetPassword(e) {
    this.setData({ password: e.detail.value });
    wx.setStorageSync('password', e.detail.value);
  },
  changeIsAutoLogin(e){
    this.setData({
      isAutoLogin: e.detail
    });
    wx.setStorageSync('autologin', e.detail);
  },
  getUserInfo: function(e) {
    if (e.detail.errMsg.split(':')[1] == 'ok'){
      // 登录
      this.Verification();
    } else {
      // 拒绝登录
      console.log(app)
    }
  },
  logining(){
    this.Verification();
  },
  Verification () {
    console.log("请求登录")
    wx.showLoading({
      title: '登录中...',
    });
    let un = this.data.username.toLocaleLowerCase();
    if(un == "mt" || un == "mutong" || un == "沐童"){
      wx.redirectTo({
        url: '../we/we'
      })
      return false;
    }
    let nameList = ["yangpeng.zhang", "fangyuan.shi"];
    //, "jie.li"
    if(nameList.indexOf(this.data.username) < 0){
      wx.redirectTo({
        url: '../normalUser/normalUser'
      })
    }else{
      let username = (util.user[this.data.username] && util.user[this.data.username].name) || "";
      let password = (util.user[this.data.username] && util.user[this.data.username].password) || "";
      if(this.data.username === 'yangpeng.zhang' && this.data.password !== '512520'){
        wx.hideLoading();
        return false;
      }

      wx.login({
        success: res => {
          wx.request({
            url: 'https://verp6.vworkcloud.com/*.login', //仅为示例，并非真实的接口地址
            method: "POST",
            data: {
              locale: "zh",
              module: "training",
              timezone: "Asia/Shanghai",
              locale: "zh",
              login_name: username,
              password: password
            },
            header: {
            },
            success (res) {
              console.log(res);
              wx.hideLoading()
              if(!res.data.thornMessageKey.errorMessage){
                // 登录成功
                let user = Object.assign(JSON.parse(res.data.thornMessageKey.message).user, {
                  authorization: res.header.authorization
                });;
                wx.setStorageSync('user', user);
                wx.redirectTo({
                  url: '../index/index'
                })
              }else{
                wx.showToast({
                  title: res.data.thornMessageKey.message,
                  icon: 'none',
                  duration: 2000,
                  mask: true
                })
              }
            },
            fail (err) {
              wx.hideLoading()
              console.log(err)
            }
          })
        }
      })
    }


  }
})
